package com.yhxd.modular.news.controller;

import com.querydsl.core.types.Order;
import com.querydsl.core.types.OrderSpecifier;
import com.yhxd.modular.news.entity.BannerImages;
import com.yhxd.modular.news.entity.QBannerImages;
import com.yhxd.modular.news.service.BannerImagesService;
import com.yhxd.modular.system.vo.ResultVo;
import io.swagger.annotations.Api;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.querydsl.QPageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;


@Controller
@RequestMapping(value = "/banner")
@Api
public class BannerImagesController {

    @Autowired
    private BannerImagesService bannerImagesService;

    @PostMapping(value = "/get_list")
    @ResponseBody
    public ResultVo getList() {
        try {
            Page<BannerImages> resourcePage = this.bannerImagesService.findPageBannerImages(PageRequest.of(0, 5, Sort.Direction.ASC, "sortNum"));
            return ResultVo.success(resourcePage);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVo.error(e.getMessage());
        }
    }

    /**
     * 跳转到列表页
     * @return
     */
    @GetMapping(value = "/list")
    @RequiresPermissions("banner_list")
    public String list() {
        return "mng/banner/banner_list";
    }

    /**
     * 查询列表数据
     * @param page
     * @param limit
     * @param dto
     * @return
     */
    @PostMapping(value = "/find_banner_list")
    @RequiresPermissions("find_banner_list")
    @ResponseBody
    public ResultVo findList(Integer page, Integer limit, BannerImages dto) {
        try {
            QPageRequest pageRequest = QPageRequest.of(page - 1, limit, new OrderSpecifier(Order.ASC, QBannerImages.bannerImages.sortNum));
            Page<BannerImages> resourcePage = this.bannerImagesService.findAll(pageRequest, dto);
            return ResultVo.success(resourcePage);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVo.error(e.getMessage());
        }
    }

    /**
     * 跳转到新增页
     * @return
     */
    @GetMapping(value = "/toAdd")
    @RequiresPermissions("banner_add")
    public String toAdd() {
        return "mng/banner/banner_add";
    }

    /**
     * 新增
     * @return
     */
    @PostMapping(value = "/add")
    @RequiresPermissions("banner_add")
    @ResponseBody
    public ResultVo add(BannerImages bannerImages) {
        try {
            this.bannerImagesService.saveOrUpdate(bannerImages);
            return ResultVo.success(null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVo.error(e.getMessage());
        }
    }

    /**
     * 跳转到编辑页
     * @return
     */
    @GetMapping(value = "/toEdit/{id}")
    @RequiresPermissions("banner_edit")
    public String toEdit(Model model, @PathVariable("id") Long id) {
        BannerImages bannerImages = this.bannerImagesService.findById(id);
        model.addAttribute("banner", bannerImages);
        return "mng/banner/banner_edit";
    }

    /**
     * 编辑
     * @return
     */
    @PostMapping(value = "/edit")
    @RequiresPermissions("banner_edit")
    @ResponseBody
    public ResultVo edit(BannerImages bannerImages) {
        try {
            this.bannerImagesService.saveOrUpdate(bannerImages);
            return ResultVo.success(null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVo.error(e.getMessage());
        }
    }

    /**
     * 删除
     * @return
     */
    @GetMapping(value = "/del/{id}")
    @RequiresPermissions("banner_del")
    @ResponseBody
    public ResultVo del(@PathVariable("id") Long id) {
        try {
            this.bannerImagesService.deleteById(id);
            return ResultVo.success(null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVo.error(e.getMessage());
        }
    }



}
